package com.chuanyang.driverh.mainmy;


import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseActivity;
import com.chuanyang.driverh.base.OnCustomListener;
import com.chuanyang.driverh.mainmy.adapter.AddCarTeamAdapter;
import com.chuanyang.driverh.responseresult.CarTeamData;
import com.chuanyang.driverh.responseresult.CarTeamResult;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.LogUtils;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;
import com.chuanyang.driverh.utils.widget.SystemBarTintManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;



/**
 *
 */

public class AddCarTeamActivity extends BaseActivity {
    protected SystemBarTintManager tintManager;

    private EditText et_search_driver;
    private ListView lv_searchcaream_list;
    private TextView tv_et_search_carteam;
    private TextView tv_et_search_shipteam;
    private ImageView iv_search_back;


    private boolean isCarTeam = true;
    private AddCarTeamAdapter adapter;
    private List<CarTeamData.DataBean> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_act_searchteam);
        setStatusBarColor();
        setStatusBarSpace();
        initView();
        initData();
        initListener();
    }

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {


    }

    private void initView() {
        iv_search_back = (ImageView) findViewById(R.id.iv_search_back);
        tv_et_search_carteam = (TextView) findViewById(R.id.tv_et_search_carteam);
        tv_et_search_shipteam = (TextView) findViewById(R.id.tv_et_search_shipteam);
        et_search_driver = (EditText) findViewById(R.id.et_search_driver);
        lv_searchcaream_list = (ListView) findViewById(R.id.lv_searchcaream_list);
        et_search_driver.setHint("搜索车队");

    }

    private void initData() {
        list = new ArrayList();
        adapter = new AddCarTeamAdapter(this, list);
        lv_searchcaream_list.setAdapter(adapter);

    }

    private void initListener() {
        iv_search_back.setOnClickListener(this);
        tv_et_search_carteam.setOnClickListener(this);
        tv_et_search_shipteam.setOnClickListener(this);
        findViewById(R.id.base_titlebar_ensure).setOnClickListener(this);

        et_search_driver.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_UNSPECIFIED || actionId == EditorInfo.IME_ACTION_SEARCH) {
                    /*隐藏软键盘*/
                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (inputMethodManager.isActive()) {
                        inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
                    }
                    if (TextUtils.isEmpty(et_search_driver.getText().toString())) {
                        return false;
                    }
                    getTeam(et_search_driver.getText().toString());
                    return true;
                }
                return false;
            }
        });

        adapter.setOnCustomListener(new OnCustomListener() {
            @Override
            public void onCustomerListener(View v, int position) {
                driveradd(list.get(position));
            }
        });

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.base_titlebar_ensure:
                if (!TextUtils.isEmpty(et_search_driver.getText().toString())) {
                    getTeam(et_search_driver.getText().toString());
                }
                break;
            case R.id.iv_search_back:
                finish();
                break;
            case R.id.tv_et_search_carteam:
                isCarTeam = true;
                et_search_driver.setHint("搜索车队");
                tv_et_search_carteam.setBackgroundResource(R.drawable.rb_shape_person_team_left_uncheck);
                tv_et_search_shipteam.setBackgroundResource(R.drawable.rb_shape_person_team_right_uncheck);
                break;
            case R.id.tv_et_search_shipteam:
                isCarTeam = false;
                et_search_driver.setHint("搜索船队");
                tv_et_search_carteam.setBackgroundResource(R.drawable.rb_shape_person_team_left_check);
                tv_et_search_shipteam.setBackgroundResource(R.drawable.rb_shape_person_team_right_check);
                break;
        }
    }



    private void getTeam(String s) {
        Map<String,String> params=new HashMap<String, String>();
        if (isCarTeam) {
            params.put("fleetType", "2");
        } else {
            params.put("fleetType", "3");
        }
        params.put("fleetState", "1");
        params.put("findField", s);
        MyHttpUtil.selectFleetList(activityContext,params, new MyCallback<CarTeamResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(CarTeamResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    list = result.getData().getData();
                    adapter.setList(list);
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });
    }

    private void driveradd(CarTeamData.DataBean bean) {
        DialogUtil.showLoadingDialog(activityContext,null);
        Map<String,String> params=new HashMap<String, String>();
        params.put("fleetDriverState", "4");
        params.put("fleetId", bean.getId() + "");
        params.put("driverId", SharedPrefUtil.getUserId());
        MyHttpUtil.driveradd(activityContext,params, new MyCallback<Result>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    if(isCarTeam) {
                      PrintUtil.toast(activityContext,"已发送申请，等待车队长确认");
                    }
                    else {
                        PrintUtil.toast(activityContext,"已发送申请，等待船队长确认");

                    }
                    onBackPressed();
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });


    }







    protected void setStatusBarColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintColor(getResources().getColor(R.color.gray_cc));
        }
    }
    private void setStatusBarSpace() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            View statusBar = findViewById(R.id.serach_status_padding);
            LogUtils.i("tint" + tintManager.getConfig().getStatusBarHeight());
            statusBar.setPadding(0, tintManager.getConfig()
                    .getStatusBarHeight(), 0, 0);
        }
    }
}
